#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnGetChars_clicked()
{
    QString str = ui->editStr->text();
    if(str.isEmpty())
        return;

    ui->plainTextEdit->clear();

    for(qint16 i = 0; i < str.size(); ++i)
    {
        QChar ch = str.at(i);
        char16_t uniCode = ch.unicode();
        QString chStr(ch);
        QString info = ch + QString::asprintf("\t, unicode : 0x%X",uniCode);
        ui->plainTextEdit->appendPlainText(info);
    }
}

void Widget::on_btnCharJudge_clicked()
{
    QString str = ui->editChar->text();
    if(str.isEmpty())
        return;
    ui->plainTextEdit->clear();
    QChar ch = str.at(0);
    char16_t uniCode = ch.unicode();
    QString info = ch + QString::asprintf("\t, unicode : 0x%X",uniCode);
    ui->plainTextEdit->appendPlainText(info);

    ui->chkDigit->setChecked(ch.isDigit());
    ui->chkLetter->setChecked(ch.isLetter());
    ui->chkLetterOrNumber->setChecked(ch.isLetterOrNumber());
    ui->chkLower->setChecked(ch.isLower());
    ui->chkMark->setChecked(ch.isMark());
    ui->chkPunct->setChecked(ch.isPunct());
    ui->chkSpace->setChecked(ch.isSpace());
    ui->chkSymbol->setChecked(ch.isSymbol());
    ui->chkUpper->setChecked(ch.isUpper());
}


void Widget::on_btnConvLatin1_clicked()
{
    QString str = "Demo";
    ui->plainTextEdit->clear();
    ui->plainTextEdit->appendPlainText(str);
    QChar ch = QChar::fromLatin1('P');
    str[0] = ch;
    ui->plainTextEdit->appendPlainText("\n" + str);
}

void Widget::on_btnConvUTF16_clicked()
{
    QString str = "Hello 北京";
    ui->plainTextEdit->clear();
    ui->plainTextEdit->appendPlainText(str);
    str[6] = QChar(0x9752);
    str[7] = QChar::fromUcs2(0x5C9B);
    ui->plainTextEdit->appendPlainText("\n" + str);
}

void Widget::on_btnCompare_clicked()
{
    QString str = "他们来自河南或河北";
    ui->plainTextEdit->clear();
    ui->plainTextEdit->appendPlainText(str);
    QString HuStr = "河to湖";
    QChar He = QChar::fromUcs2(HuStr[0].unicode());
    QChar Hu = QChar(HuStr[3]);
    for(int i = 0; i < str.size(); ++i)
    {
        if(str[i] == He)
            str[i] = Hu;
    }

    ui->plainTextEdit->appendPlainText("\n" + str);
}

void Widget::on_btnClear_clicked()
{
    ui->plainTextEdit->clear();
}

